package com.atguigu.controller;


import com.atguigu.entity.UserInfo;
import com.atguigu.entity.bo.LoginBo;
import com.atguigu.entity.bo.RegisterBo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5Util;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpSession session){
        //这里本来需要调用第三方云短信的API给mobile发送短信
        //模拟短信验证码
        String code = "1111";
        //将短信存储到会话域session中用于校验
        session.setAttribute("CODE",code);
        //响应发送成功
        return Result.ok();
    }

    /**
     * 短信校验
     * @param registerBo 业务对象,javaBean
     * @param session 会话域
     * @return
     */
    @RequestMapping("/register")
    public Result register(@RequestBody RegisterBo registerBo, HttpSession session){
        //校验验证码是否正确
        String codeInSession = (String) session.getAttribute("CODE");
        if (!registerBo.getCode().equals(codeInSession)){
            //返回结果集数据,result全局统一返回类
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //校验手机号是否存在
        UserInfo userInfo = userInfoService.findUserInfoByPhone(registerBo.getPhone());
        if (userInfo !=null){
            return Result.build(null,ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //密码加密
        registerBo.setPassword(MD5Util.encrypt(registerBo.getPassword()));
        //创建用户对象封装用户信息
        userInfo = new UserInfo();
        BeanUtils.copyProperties(registerBo,userInfo);
        userInfo.setStatus(1);
        //注册,添加用户信息到数据库中,只需要电话号码,状态码和用户姓名
        userInfoService.save(userInfo);
        //返回一个空的结果集
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginBo loginBo ,HttpSession session){
        //判断手机号是否正确
        UserInfo userInfo = userInfoService.findUserInfoByPhone(loginBo.getPhone());
        if (userInfo == null) {
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
        //判断密码是否正确
        if (!MD5Util.encrypt(loginBo.getPassword()).equals(userInfo.getPassword())) {
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }

        //校验账户是否被禁用
        if (userInfo.getStatus() == 0) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }

        //登录成，将用户信息存储到session中,后续关注列表显示需要从session中获取当前登录用户信息
        session.setAttribute("USER",userInfo);

        //封装响应数据
        Map responseMapping = new HashMap();
        responseMapping.put("nickName",userInfo.getNickName());
        responseMapping.put("phone",userInfo.getPhone());

        return Result.ok(responseMapping);
    }

    //注销功能
    @RequestMapping("/logout")
    public Result logout(HttpSession session){
        //销毁session，当前用户保存的数据全部销毁
        session.invalidate();
        return Result.ok();
    }

}
